package com.gps.track;


import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.appengine.utils.GsonUtils;
import com.datatypes.Settings;
import com.datatypes.Track;
import com.datatypes.User;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.sporteamws.R;

public class GpsShowMap extends MapActivity {

	MapView map;
	Chronometer timer;
	TextView speed;
	TextView distance;
	TextView calories;
	ImageView signal;
	ImageButton button;

	Track ghostTrack = null;

	MapController contorller = null;

	DrawPathOverlay drawPath = null;

	Settings settings = null;

	User user = null;
	

	@Override
	protected boolean isRouteDisplayed() {
		return true;
	}

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		/** set layout **/
		setContentView(R.layout.runningscreanteam);
		map = (MapView) findViewById(R.id.mvMain);
		speed = (TextView) findViewById(R.id.speed);
		distance = (TextView) findViewById(R.id.distance);
		signal = (ImageView) findViewById(R.id.gps_signal);
		timer = (Chronometer) findViewById(R.id.timer);
		button = (ImageButton) findViewById(R.id.activityButton);
		calories = (TextView) findViewById(R.id.calories);
		/****************/

		button.setVisibility(View.INVISIBLE);

		map.setBuiltInZoomControls(true);
		contorller = map.getController();
		contorller.setZoom(18);
		signal.setVisibility(View.INVISIBLE);
		settings = new Settings(); // TODO change
		user = GsonUtils.ConvertGsonStringToUser(getIntent().getExtras()
				.getString("USER"));

		String trackStr = getIntent().getExtras().getString("GHOST_TRACK");
		ghostTrack = GsonUtils.ConvertGsonStringToTrack(trackStr);
		drawPath = new DrawPathOverlay(ghostTrack.GetGeoPtrList(), true, true,
				true, contorller, settings.getGhostTrackColor());
		map.getOverlays().add(drawPath);
		contorller.setCenter(DrawPathOverlay.toGeoPoint(ghostTrack
				.GetGeoPtrList().get(ghostTrack.GetGeoPtrList().size() - 1)));
		ghostStatistics(ghostTrack, true);

	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
/*
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("post on facebook").setIcon(R.drawable.post);
		menu.add("send to friend").setIcon(
				android.R.drawable.ic_menu_mylocation);
		return super.onCreateOptionsMenu(menu);
	}
*/
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		String title = item.getTitle().toString();

		if (title.equals("post on facebook")) {

		} else if (title.equals("send to friend")) {
			//TODO open friends list
		}
		return super.onOptionsItemSelected(item);
	}

	private void ghostStatistics(Track track, boolean showTime) {
		distance.setText(String.format("%.02f",
				(track.getDistance() > 1000) ? (track.getDistance()) / 1000
						: track.getDistance()));
		speed.setText(String.format("%.02f", track.GetAvgSpeed()));
		calories.setText(String.format("%.02f", track.GetCalories()));
		if (showTime)
			timer.setBase(SystemClock.elapsedRealtime() - track.getTime());
	}

}
